package com.hzncc.flowable_diboot.controller.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.controller.BaseCrudRestController;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.util.IamSecurityUtils;
import com.hzncc.flowable_diboot.dto.CustomConnectorDTO;
import com.hzncc.flowable_diboot.entity.CustomConnector;
import com.hzncc.flowable_diboot.service.CustomConnectorService;
import com.hzncc.flowable_diboot.util.ConstantUtil;
import com.hzncc.flowable_diboot.vo.CustomConnectorDetailVO;
import com.hzncc.flowable_diboot.vo.CustomConnectorListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户联系人 相关rest接口
 *
 * @author MyName
 * @version 1.0
 * @date 2024-05-15
 * Copyright © MyCorp
 */
@Slf4j
@RequestMapping("/custom-connector")
@BindPermission(name = "客户联系人")
@RestController
public class CustomConnectorController extends BaseCrudRestController<CustomConnector> {

    @Autowired
    private CustomConnectorService customConnectorService;

    /**
     * 查询资源对象的列表VO记录
     * <p>
     * url请求参数示例: ?fieldA=abc&pageSize=20&pageIndex=1&orderBy=id
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping()
    public JsonResult<List<CustomConnectorListVO>> getListVOMapping(CustomConnectorDTO queryDto, Pagination pagination, String type) throws Exception {
        return super.getViewObjectList(queryDto, pagination, CustomConnectorListVO.class);
        //return customConnectorService.selectCustomConnector(queryDto,pagination,type);
    }

    /**
     * 根据id查询资源对象的详情VO
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult<CustomConnectorDetailVO> getDetailVOMapping(@PathVariable("id") String id) throws Exception {
        CustomConnectorDetailVO detailVO = customConnectorService.getViewObject(id, CustomConnectorDetailVO.class);
        return JsonResult.OK(detailVO);
    }


    /**
     * 创建资源对象数据
     *
     * @param customConnector
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping()
    public JsonResult<?> createModelMapping(@RequestBody CustomConnector customConnector) throws Exception {
        customConnector.setUserId(IamSecurityUtils.getCurrentUserId()); // 默认负责人为创建人
        boolean success = customConnectorService.createEntity(customConnector);
        log.debug("新建数据 客户联系人:{} {}", customConnector.getId(), success ? "成功" : "失败");
        return JsonResult.OK(customConnector.getId());
    }

    /**
     * 批量创建资源对象数据
     *
     * @param customConnectorList
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_BATCH_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch")
    public JsonResult<?> batchCreateModelMapping(@RequestBody List<CustomConnector> customConnectorList) throws Exception {
        boolean success = customConnectorService.createEntities(customConnectorList);
        log.debug("批量新建数据 客户联系人 {}", success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据id更新资源对象
     *
     * @param customConnector
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateModelMapping(@PathVariable("id") String id, @RequestBody CustomConnector customConnector) throws Exception {
        boolean success = customConnectorService.updateEntity(customConnector);
        log.debug("更新数据 客户联系人:{} {}", id, success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = ConstantUtil.PERMISSION_DELETE_LABEL, code = ConstantUtil.PERMISSION_DELETE)
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteModelMapping(@PathVariable("id") String id) throws Exception {
        boolean success = customConnectorService.deleteEntity(id);
        log.debug("删除数据 客户联系人:{} {}", id, success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据ids集合删除资源对象
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_BATCH_DELETE)
    @BindPermission(name = ConstantUtil.PERMISSION_DELETE_LABEL, code = ConstantUtil.PERMISSION_DELETE)
    @PostMapping("/batch-delete")
    public JsonResult<?> deleteEntityMapping(@RequestBody List<String> ids) throws Exception {
        boolean success = customConnectorService.deleteEntities(ids);
        log.debug("批量删除数据 客户联系人:{} {}", ids, success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 变更负责人
     *
     * @return
     * @author 陈广煜
     * @date 2024/5/22
     */
    @Log(operation = OperationCons.LABEL_WRITE)
    @BindPermission(name = OperationCons.LABEL_WRITE, code = OperationCons.CODE_WRITE)
    @GetMapping("/change")
    public JsonResult<?> change(@RequestParam String userId, @RequestParam String id) {
        customConnectorService.change(userId, id);
        return JsonResult.OK();
    }

    /**
     * 获取客户联系人
     *
     * @param customerId 客户id
     * @return
     * @author 陈广煜
     * @date 2024/7/3
     */
    @Log(operation = OperationCons.LABEL_READ)
    @BindPermission(name = OperationCons.LABEL_READ, code = OperationCons.CODE_READ)
    @GetMapping("/label/{customerId}")
    public JsonResult<?> labelValue(@PathVariable String customerId) {
        QueryWrapper<CustomConnector> qw = new QueryWrapper<>();
        qw.lambda()
                .select(CustomConnector::getConnector, CustomConnector::getConnectWay)
                .eq(CustomConnector::getCustomerId, customerId);
        List<CustomConnector> entityList = customConnectorService.getEntityList(qw);
        return JsonResult.OK(entityList);
    }
}